﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using fuzhu;
using Newtonsoft.Json;
using System.Data;
using System.Net;
using System.IO;
//using System.Xml.Linq;

namespace LKU8.shoukuan
{
    public static class SMLWeb
    {

        //public static void setContr(string connstr)
        //{
        //    canshu.conStr = connstr;
        //}

        #region 对接基础类
        public class ClsXieTong
        {
            public string Clx { get; set; }
            public int Iid { get; set; }




        }
        

        #region 返回数据类
        public class FanHuiData
        {
            public int code { get; set; }
            public string message { get; set; }
        }
        #endregion

      


        #endregion

        /// <summary>
        /// api操作方法
        /// </summary>
        /// <param name="cMethod">方法，put post，delete</param>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public static string PostApi(string cMethod, string url, string cJason)
        { 
            
            //json参数
            string jsonParam = cJason;
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = cMethod;
            request.ContentType = "application/json;charset=UTF-8";
            byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
            int length = byteData.Length;
            request.ContentLength = length;
            Stream writer = request.GetRequestStream();
            writer.Write(byteData, 0, length);
            writer.Close();
            var response = (HttpWebResponse)request.GetResponse(); 

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                return responseString.ToString();
            }
            else
            {

                return response.StatusDescription; 
            }
        }


        #region 推送
        /// <summary>
        /// 协同推送
        /// </summary>
        /// <param name="ctrayNo"></param>
        /// <returns></returns>
        public static string Post(string clx,int id)
        {
            string cError = "";
            try
            {
               ClsXieTong clsInvClass = new ClsXieTong();
                clsInvClass.Clx =clx;
                clsInvClass.Iid =id;
                 
                    var jSetting = new JsonSerializerSettings();
                    jSetting.NullValueHandling = NullValueHandling.Include;
                    string json = JsonConvert.SerializeObject(clsInvClass, jSetting);


                    string cRe = PostApi("POST", "/api/u8xietong", json);   //url返回的值  


                    //结果返回类
                    //JsonConvert,将jason转换成类，返回成功，更新u8已上传状态
                    FanHuiData obj = JsonConvert.DeserializeObject<FanHuiData>(cRe);
                    int iStatus = obj.code;
                    string cMsg = obj.message;
                    return cMsg;
                
            }
            catch (Exception ex)
            {
                return ex.Message;

            }
          

        }
        #endregion




    }
}
